﻿Imports Windows.UI.Input

' The Items Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234233

''' <summary>
''' A page that displays a collection of item previews.  In the Split Application this page
''' is used to display and select one of the available groups.
''' </summary>
Public NotInheritable Class MainPage
    Inherits Common.LayoutAwarePage

    Private Property GameList() As New ObservableCollection(Of GameViewModel)
    Private m_loaded As Boolean = False
    Private m_appBar As AppBar

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        ' Me.m_appBar = Me.BottomAppBar
    End Sub

    ''' <summary>
    ''' Populates the page with content passed during navigation.  Any saved state is also
    ''' provided when recreating a page from a prior session.
    ''' </summary>
    ''' <param name="navigationParameter">The parameter value passed to <see cref="Frame.Navigate"/>
    ''' when this page was initially requested.
    ''' </param>
    ''' <param name="pageState">A dictionary of state preserved by this page during an earlier
    ''' session.  This will be null the first time a page is visited.</param>
    Protected Overrides Sub LoadState(navigationParameter As Object, pageState As Dictionary(Of String, Object))
        ' TODO: Assign a bindable collection of items to Me.DefaultViewModel("Items")
        Dim lst As New List(Of GameViewModel)
        For Each item In g_gameList
            lst.Add(New GameViewModel(item.Name, item.Author, item.Image))
        Next
        Me.GameList = New ObservableCollection(Of GameViewModel)(lst)
        Me.DefaultViewModel("Items") = Me.GameList
    End Sub

    Protected Overrides Sub SaveState(pageState As Dictionary(Of String, Object))
        MyBase.SaveState(pageState)
    End Sub

    Private Sub itemView_Loaded(sender As Object, e As RoutedEventArgs)
        Try
            If TypeOf sender Is ListView Then
                CType(sender, ListView).SelectedItem = Nothing
            ElseIf TypeOf sender Is GridView Then
                CType(sender, GridView).SelectedItem = Nothing
            End If
            Me.m_loaded = True
        Catch ex As Exception
            'Me.m_loaded = False
            Exit Sub
        End Try
    End Sub

    Private Sub itemView_ItemClick(sender As Object, e As ItemClickEventArgs)
        Dim gv = CType(e.ClickedItem, GameViewModel)
        Debug.WriteLine("Item {0} clicked.", gv.Title)
        g_gamePkg = g_gameList.Find(Function(item) item.Name = gv.Title)
        Window.Current.Content = New GameViewPage()
    End Sub

    Private Sub itemView_SelectionChanged(sender As Object, e As SelectionChangedEventArgs)
        If Me.m_loaded Then
            If e.AddedItems.Count > 0 And sender.Visibility <> Visibility.Collapsed Then
                'If Me.BottomAppBar.Visibility = Windows.UI.Xaml.Visibility.Visible Then
                If Me.BottomAppBar.IsOpen = True Then
                    Me.BottomAppBar.IsOpen = False
                    'Await Me.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High,
                    '    Sub() Me.BottomAppBar.IsOpen = False)
                Else
                    ' Me.BottomAppBar.Visibility = Windows.UI.Xaml.Visibility.Visible
                    Me.BottomAppBar.IsOpen = True
                    Me.Btn_GameSettings.Tag = e.AddedItems.FirstOrDefault()
                    'Await Me.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High,
                    '    Sub() Me.BottomAppBar.IsOpen = True)
                End If
            ElseIf e.RemovedItems.Count > 0 And sender.Visibility <> Visibility.Collapsed Then
                Me.BottomAppBar.IsOpen = False
                'Await Me.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High,
                '       Sub() Me.BottomAppBar.IsOpen = False)
            End If
        End If
    End Sub

    Private Sub itemView_PointerPressed(sender As Object, e As PointerRoutedEventArgs)
        Dim properties = PointerPoint.GetCurrentPoint(e.Pointer.PointerId).Properties
        If properties.IsRightButtonPressed = True Then
            e.Handled = True
        End If
    End Sub

    Private Sub AppBar_Closed(sender As Object, e As Object)
        'Me.BottomAppBar.Visibility = Windows.UI.Xaml.Visibility.Collapsed
    End Sub

    Private Sub backButton_Click(sender As Object, e As RoutedEventArgs)
        Application.Current.Exit()
    End Sub

    Private Sub Btn_GameSettings_PointerPressed(sender As Object, e As PointerRoutedEventArgs)
        Dim gv As GameViewModel = CType(sender, Button).Tag
        g_gamePkg = g_gameList.Find(Function(item) item.Name = gv.Title)
        Me.Frame.Navigate(GetType(SplitPageGameSettings), g_gamePkg)
    End Sub

    Private Sub Btn_GameSettings_Click(sender As Object, e As RoutedEventArgs)
        Dim gv As GameViewModel = CType(sender, Button).Tag
        g_gamePkg = g_gameList.Find(Function(item) item.Name = gv.Title)
        Me.Frame.Navigate(GetType(SplitPageGameSettings), g_gamePkg)
    End Sub

End Class